﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace EmulatorLANForwarder
{
    class Program
    {
        private static StreamReader r;
        private static StreamWriter w;
        private static TcpClient cl;

        private static TcpClient FW;
        private static StreamReader FWr;
        private static StreamWriter FWw;

        static void Main(string[] args)
        {
            
            Console.WriteLine("Port?:");
            int port = int.Parse(Console.ReadLine());
            Console.WriteLine("NET MF IP?:");
            string ip = Console.ReadLine();

            TcpListener l = new TcpListener(IPAddress.Loopback, port);
            l.Start();

            while ( true )
            {
                try
                {
                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine("LISTENING");
                    cl = l.AcceptTcpClient();
                    r = new StreamReader(cl.GetStream());
                    w = new StreamWriter(cl.GetStream());
                    
                    FW = new TcpClient(ip, port);
                    FWr = new StreamReader(FW.GetStream());
                    FWw = new StreamWriter(FW.GetStream());

                    Console.WriteLine();
                    Console.WriteLine("REQUEST");

                    char lastChar = 'a';
                    bool seenLineBreak = false;
                    bool abort = false;
                    while (cl.Connected)
                    {
                        int nb = r.Read();
                        if (nb == -1)
                        {
                            abort = true;
                            break;
                        }
                        Console.Write((char)nb);
                        FWw.Write((char)nb);
                        FWw.Flush();
                        if ( (char)nb == '\n' && lastChar == '\r')
                        {
                            if ( seenLineBreak )
                            {
                                break;
                            }
                            seenLineBreak = true;
                        }
                        if (lastChar != '\n' && lastChar != '\r') seenLineBreak = false;
                        
                        lastChar = (char) nb;
                    }

                    if (abort) continue;

                    Console.WriteLine();
                    Console.WriteLine("RESPONSE");

                    while (FW.Connected)
                    {
                        int re = FWr.Read();
                        char nb = (char)re;
                        if (re == -1) break;
                        Console.Write((char)nb);
                        w.Write(nb);
                        w.Flush();
                    }

                    
                }catch(Exception)
                {
                }
                finally
                {
                    try
                    {
                        r.Close();
                        w.Close();
                        cl.Close();
                    }
                    catch (Exception)
                    {

                    }
                    try
                    {
                        FWr.Close();
                        FWw.Close();
                        FW.Close();
                    }catch(Exception)
                    {
                        
                    }
                }

            }

        }

    }
}
